/*
 * @FileName: InterfaceUtil.java
 * 
 * @Author
 * 
 * @Description:
 * 
 * @Date 2016年1月5日 上午9:29:02
 * 
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.util;

import cn.hpclub.server.bean.OrgNode;
import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.Organization;
import cn.hpclub.server.security.ShiroPrincipal;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import org.apache.shiro.SecurityUtils;

import java.util.*;
import java.util.Map.Entry;

/**
 * Class description goes here
 *
 * @author
 */
public class InterfaceUtil{
    /*
     * 通过当前的机构id，获取下级机构的id，包括自身
     */
    public static int ID_TOP     = 0;
    public static int ID_CHENGDU = 0;

    static public List<String> getOrganization(){
        int orgId = getOrgId();
        List<String> list = getOrganization(orgId);
        return list;
    }

    /*
     * 根据机构id，获取下级机构id，包括自身
     */
    static public List<String> getOrganization(int id){

        List<String> result = new ArrayList<String>();
        try{
            Object[] p = { id, id, id, id };
            Page<Record> pager = Db.paginate(1, 100000, " select id",
                                             "from organization where id =?"
                                                     + " or id in (SELECT id FROM `organization` where parentid=?)"
                                                     + " or parentid in (SELECT id FROM `organization` where parentid=?)"
                                                     + " or parentid in (SELECT id FROM `organization` where parentid in (SELECT id FROM `organization` where parentid=?))",
                                             p);
            List<Record> pager_list = pager.getList();
            for(int i = 0; i < pager_list.size(); i++){
                result.add(String.valueOf(pager_list.get(i).getInt("id")));
            }

        }
        catch(Exception e){
            e.printStackTrace();
        }
        return result;

    }



    /*
     * 根据机构id，获取下级机构id，包括自身
     */
    static public List<Integer> getOrganizationReturnIntegerList(int id){

        List<Integer> result = new ArrayList<>();
        try{
            Object[] p = { id, id, id, id };
            Page<Record> pager = Db.paginate(1, 100000, " select id",
                    "from organization where id =?"
                            + " or id in (SELECT id FROM `organization` where parentid=?)"
                            + " or parentid in (SELECT id FROM `organization` where parentid=?)"
                            + " or parentid in (SELECT id FROM `organization` where parentid in (SELECT id FROM `organization` where parentid=?))",
                    p);
            List<Record> pager_list = pager.getList();
            for(int i = 0; i < pager_list.size(); i++){
                result.add(pager_list.get(i).getInt("id"));
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }
    /*
     * 根据机构id，获取下级机构id，包括自身
     */
    static public Map<Integer,Integer> getOrganizationReturnIntegerMap(int id){

        Map<Integer,Integer> result = new HashMap<>();
        try{
            Object[] p = { id, id, id, id };
            Page<Record> pager = Db.paginate(1, 400000, " select id",
                    "from organization where id =?"
                            + " or id in (SELECT id FROM `organization` where parentid=?)"
                            + " or parentid in (SELECT id FROM `organization` where parentid=?)"
                            + " or parentid in (SELECT id FROM `organization` where parentid in (SELECT id FROM `organization` where parentid=?))",
                    p);
            List<Record> pager_list = pager.getList();
            for(int i = 0; i < pager_list.size(); i++){
                int orgId = pager_list.get(i).getInt("id");
                result.put(orgId,orgId);
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }





    /*
     * sql语句:根据机构id，获取下级机构id，包括自身
     */
    static public String getOrganizationSql(int id){
        return new StringBuffer("select id from organization where id =?")
                .append(" or id in (SELECT id FROM `organization` where parentid=?)")
                .append(" or parentid in (SELECT id FROM `organization` where parentid=?)")
                .append(" or parentid in (SELECT id FROM `organization` where parentid in (SELECT id FROM `organization` where parentid=?))")
                .toString();
    }

    /*
     * sql语句:根据机构id，获取下级机构id，包括自身
     */
    static public String getOrganizationLevel1Sql(){

        return new StringBuffer("select " + Organization.ID + "," + Organization.DISPLAYNAME + " from "
                + Constant.Table.ORGANIZATION + " where " + Organization.PARENTID + "=" + getIdTop())

                        .toString();
    }

    /*
     * sql语句:根据机构id，获取向下指定机构id集合， id:当前机构id
     * level:向下等级，0为当前，1为当前+向下1级，2为当前+向下2级，3为当前+向下3级 child:是否需要最底层节点
     */
    static public String getChlidOrgIdSql(int id, int level, boolean child){

        String sql_parent = new StringBuffer("(select " + Organization.ID + " from " + Constant.Table.ORGANIZATION
                + " where " + Organization.PARENTID).toString();

        StringBuffer sql = new StringBuffer(
                "(select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where ");

        if(!child){
            level = Constant.MAX_ORG_LEVEL - getOrgLevel(id) - 1;
        }

        if(level >= 0){
            sql.append(Organization.ID + "=" + id);
        }
        if(level >= 1){
            sql.append(" or " + Organization.ID + " in " + sql_parent + "=" + id + ")");
        }
        if(level >= 2){
            sql.append(" or " + Organization.PARENTID + " in " + sql_parent + "=" + id + ")");
        }
        if(level >= 3){
            sql.append(" or " + Organization.PARENTID + " in " + sql_parent + " in " + sql_parent + "=" + id + "))");
        }
        sql.append(")");

        return sql.toString();
    }

    /*
     * sql语句:根据机构id，获取向上指定机构id集合， id:当前机构id
     * level:向上等级，0为当前，1为当前+向上1级，2为当前+向上2级，3为当前+向上3级
     */
    static public String getParentOrgIdSql(int id, int level){

        String sql_parent = new StringBuffer("(select " + Organization.PARENTID + " from " + Constant.Table.ORGANIZATION
                + " where " + Organization.ID).toString();

        StringBuffer sql = new StringBuffer(
                "(select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where ");
        if(level >= 0){
            sql.append(Organization.ID + "=" + id);
        }
        if(level >= 1){
            sql.append(" or " + Organization.ID + "=" + sql_parent + "=" + id + ")");
        }
        if(level >= 2){
            sql.append(" or " + Organization.ID + "=" + sql_parent + "=" + sql_parent + "=" + id + "))");
        }
        if(level >= 3){
            sql.append(" or " + Organization.ID + "=" + sql_parent + "=" + sql_parent + "=" + sql_parent + "=" + id
                    + ")))");
        }
        sql.append(")");

        return sql.toString();
    }

    /*
     * sql语句:根据机构id，获取向上指定机构id集合， id:当前机构id
     * level:向上等级，0为当前，1为当前+向上1级，2为当前+向上2级，3为当前+向上3级
     */
    static public String getParentOfChengDuOrgIdSql(int id, int level){

        String sql_parent = new StringBuffer("(select " + Organization.PARENTID + " from " + Constant.Table.ORGANIZATION
                + " where " + Organization.ID).toString();

        StringBuffer sql = new StringBuffer(
                "(select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where ");
        if(level >= 0){
            sql.append(Organization.ID + "=" + id);
        }
        if(level >= 1){
            sql.append(" or " + Organization.ID + "=" + sql_parent + "=" + id + ")");
        }
        if(level >= 2){
            sql.append(" or " + Organization.ID + "=" + sql_parent + "=" + sql_parent + "=" + id + "))");
        }
        if(level >= 3){
            sql.append(" or " + Organization.ID + "=" + sql_parent + "=" + sql_parent + "=" + sql_parent + "=" + id
                    + ")))");
        }
        sql.append(")");

        return sql.toString();
    }

    /*
     * sql语句:根据机构id，指定获取下属某级id集合
     */
    static public String getChlidOrgByLevelSql(int id, int level){
        String sql_parent = new StringBuffer("(select " + Organization.ID + " from " + Constant.Table.ORGANIZATION
                + " where " + Organization.PARENTID).toString();

        StringBuffer sql = new StringBuffer(
                "(select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where ");
        sql.append(Organization.ID + "=" + id);
        sql.append(" or " + Organization.ID + " in " + sql_parent + "=" + id + ")");
        sql.append(" or " + Organization.PARENTID + " in " + sql_parent + "=" + id + ")");
        sql.append(" or " + Organization.PARENTID + " in " + sql_parent + " in " + sql_parent + "=" + id + "))");
        sql.append(")");

        if(level == 0){
            sql.append(" and " + Organization.CITYCODE + "=0 and " + Organization.AREACODE + "=0");
        }
        if(level == 1){
            sql.append(" and " + Organization.CITYCODE + "!=0 and " + Organization.AREACODE + "=0");
        }
        if(level == 2){
            sql.append(" and " + Organization.CITYCODE + "=" + Organization.PARENTID + " and " + Organization.CITYCODE
                    + "!=0");
        }
        if(level == 3){
            sql.append(" and " + Organization.AREACODE + "=" + Organization.PARENTID + " and " + Organization.AREACODE
                    + "!=0");
        }

        return sql.toString();
    }




    static public String getChlidOrgByLevelSqlWithOutLevel(int id){
        String sql_parent = new StringBuffer("(select " + Organization.ID + " from " + Constant.Table.ORGANIZATION
                + " where " + Organization.PARENTID).toString();

        StringBuffer sql = new StringBuffer(
                "(select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where ");
        sql.append(Organization.ID + "=" + id);
        sql.append(" or " + Organization.ID + " in " + sql_parent + "=" + id + ")");
        sql.append(" or " + Organization.PARENTID + " in " + sql_parent + "=" + id + ")");
        sql.append(" or " + Organization.PARENTID + " in " + sql_parent + " in " + sql_parent + "=" + id + "))");
        sql.append(")");
        return sql.toString();
    }




    /*
     * sql语句:根据机构id，指定获取下属某级id集合
     */
    static public String getChlidOrgByLevelSqlIncludeArea(int id, int level){
        String sql_parent = new StringBuffer("(select " + Organization.ID + " from " + Constant.Table.ORGANIZATION
                + " where " + Organization.PARENTID).toString();

        StringBuffer sql = new StringBuffer(
                "(select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where (");
        sql.append(Organization.ID + "=" + id);
        sql.append(" or " + Organization.ID + " in " + sql_parent + "=" + id + ")");
        sql.append(" or " + Organization.PARENTID + " in " + sql_parent + "=" + id + ")");
        sql.append(" or " + Organization.PARENTID + " in " + sql_parent + " in " + sql_parent + "=" + id + "))");
        sql.append(")");

        if(level == 0){
            sql.append(" and " + Organization.CITYCODE + "=0 and " + Organization.AREACODE + "=0");
        }
        if(level == 1){
            sql.append(" and " + Organization.CITYCODE + "!=0 and " + Organization.AREACODE + "=0");
        }
        if(level == 2){
            sql.append(" and " + Organization.CITYCODE + "=" + Organization.PARENTID + " and " + Organization.CITYCODE
                    + "!=0");
        }
        if(level == 3){
            sql.append(" and " + Organization.AREACODE + "=" + Organization.PARENTID + " and " + Organization.AREACODE
                    + "!=0");
        }
        sql.append(")");
        return sql.toString();
    }


    static public String joinParentOrgIdSql(String id, int level){

        String sql_parent = new StringBuffer("(select " + Organization.PARENTID + " from " + Constant.Table.ORGANIZATION
                + " where " + Organization.ID).toString();

        StringBuffer sql = new StringBuffer(
                "(select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where ");
        if(level >= 0){
            sql.append(Organization.ID + "=" + id);
        }
        if(level >= 1){
            sql.append(" or " + Organization.ID + "=" + sql_parent + "=" + id + ")");
        }
        if(level >= 2){
            sql.append(" or " + Organization.ID + "=" + sql_parent + "=" + sql_parent + "=" + id + "))");
        }
        if(level >= 3){
            sql.append(" or " + Organization.ID + "=" + sql_parent + "=" + sql_parent + "=" + sql_parent + "=" + id
                    + ")))");
        }
        sql.append(")");

        return sql.toString();
    }

    /*
     * sql语句:根据机构id，获取城市
     */
    static public String getCitySql(String id){

        StringBuffer sql = new StringBuffer(
                "(select " + Organization.DISPLAYNAME + " from " + Constant.Table.ORGANIZATION);
        sql.append(" where " + Organization.ID + "=(select " + Organization.CITYCODE + " from "
                + Constant.Table.ORGANIZATION + " where " + Organization.ID + "=" + id + "))");

        return sql.toString();
    }

    static public String getCitySql(String id, String columns){
        StringBuffer sql = new StringBuffer("(select " + columns + " from " + Constant.Table.ORGANIZATION);
        sql.append(" where " + Organization.ID + "=(select " + Organization.CITYCODE + " from "
                + Constant.Table.ORGANIZATION + " where " + Organization.ID + "=" + id + "))");

        return sql.toString();
    }

    /**
     * 根据org_id找到地市id
     * 
     * @param org_id
     * @return
     */
    static public int getCityIdByOrgId(String org_id){
        Record record = Db.findFirst(getCitySql(org_id, "id"));
        return record.getInt(Organization.ID);
    }

    /*
     * sql语句:根据机构id，获取地区
     */
    static public String getAreaSql(String id){

        StringBuffer sql = new StringBuffer(
                "(select " + Organization.DISPLAYNAME + " from " + Constant.Table.ORGANIZATION);
        sql.append(" where " + Organization.ID + "=(select " + Organization.AREACODE + " from "
                + Constant.Table.ORGANIZATION + " where " + Organization.ID + "=" + id + "))");

        return sql.toString();
    }

    /*
     * sql语句:根据机构id，获取机构树 level:向上等级，0为当前，1为当前+向上1级，2为当前+向上2级，3为当前+向上3级
     * child:是否需要最底层节点 返回:机构树结构OrgNode
     */
    static public OrgNode getOrgTree(int id, int level, boolean child){

        List<Record> pager = Db.find("select " + Organization.ID + "," + Organization.DISPLAYNAME + ","
                + Organization.PARENTID + " from " + Constant.Table.ORGANIZATION + " where " + Organization.ID + " in "
                + getChlidOrgIdSql(id, level, true) + " AND " + Organization.ENABLED + " = 1");

        OrgNode orgNode = new OrgNode(id, "", 0);
        List<OrgNode> subNode = null;
        List<OrgNode> subNode1 = null;
        for(Record r : pager){
            if(r.getInt(Organization.ID) == id){
                orgNode.setDisplayName(r.getStr(Organization.DISPLAYNAME));
                break;
            }
        }
        if(!child){
            level = Constant.MAX_ORG_LEVEL - getOrgLevel(id) - 1;
        }

        // 0级
        if(level <= 0){
            orgNode.printNode("");
            return orgNode;
        }
        // 1级
        orgNode.setSubNode(getOrgSub(orgNode, pager));
        if(level <= 1){
            orgNode.printNode("");
            return orgNode;
        }
        // 2级
        subNode = new LinkedList<OrgNode>();
        for(OrgNode temp : orgNode.getSubNode()){
            temp.setSubNode(getOrgSub(temp, pager));
            subNode.add(temp);
        }
        orgNode.setSubNode(subNode);
        if(level <= 2){
            orgNode.printNode("");
            return orgNode;
        }
        // 3级
        subNode1 = new LinkedList<OrgNode>();

        for(OrgNode temp1 : orgNode.getSubNode()){
            subNode = new LinkedList<OrgNode>();
            for(OrgNode temp2 : temp1.getSubNode()){
                temp2.setSubNode(getOrgSub(temp2, pager));
                subNode.add(temp2);
            }
            temp1.setSubNode(subNode);
            subNode1.add(temp1);
        }
        orgNode.setSubNode(subNode1);
        orgNode.printNode("");
        return orgNode;
    }

    static public List<OrgNode> getOrgSub(OrgNode orgNode, List<Record> pager){
        List<OrgNode> list = new LinkedList<OrgNode>();
        OrgNode temp = null;

        for(Record r : pager){
            if(r.getInt(Organization.PARENTID) == orgNode.getId()){
                temp = new OrgNode(r.getInt(Organization.ID), r.getStr(Organization.DISPLAYNAME),
                        r.getInt(Organization.PARENTID));

                list.add(temp);
            }
        }

        return list;
    }

    static public int getOrgLevel(int id){
        if(id == getIdTop()){
            return 0;
        }
        Record r = Db.findFirst("select distinct (select " + Organization.PARENTID + " from "
                + Constant.Table.ORGANIZATION + " where " + Organization.ID + "=?)as l1," + "(select "
                + Organization.PARENTID + " from " + Constant.Table.ORGANIZATION + " where " + Organization.ID
                + "=(select " + Organization.PARENTID + " from " + Constant.Table.ORGANIZATION + " where "
                + Organization.ID + "=?)) as l2," + "(select " + Organization.PARENTID + " from "
                + Constant.Table.ORGANIZATION + " where " + Organization.ID + "=(select " + Organization.PARENTID
                + " from " + Constant.Table.ORGANIZATION + " where " + Organization.ID + "=(select "
                + Organization.PARENTID + " from " + Constant.Table.ORGANIZATION + " where " + Organization.ID
                + "=?))) as l3" + " from " + Constant.Table.ORGANIZATION, new Object[] { id, id, id });
        if(r.getInt("l1") == getIdTop()){
            return 1;
        }
        if(r.getInt("l2") == getIdTop()){
            return 2;
        }
        if(r.getInt("l3") == getIdTop()){
            return 3;
        }
        return 4;
    }

    static public boolean isOrgChengdu(int id){

        if(Db.find("select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where " + Organization.ID
                + "=" + id + " and " + Organization.ID + " in "
                + getChlidOrgIdSql(getIdChengdu(), Constant.MAX_ORG_LEVEL, true)).size() <= 0){
            return false;
        } else{
            return true;
        }

    }

    static public boolean isOrgInChengDu(int id){
        // select id from organization where id= 167681 and CityCode = (select
        // org.id from organization as org where org.NumCode = 2 );

        if(Db.find("select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where " + Organization.ID
                + "=" + id + " and " + Organization.CITYCODE + " = "
                + "(select org.id from organization as org where org.NumCode = ?)", "2").size() <= 0){
            return false;
        } else{
            // System.out.println("orgid in chengdu:" + id);
            return true;
        }
    }

    static public boolean isOrgInHotCity(int id){
        if(isOrgInMianYang(id)){
            return true;
        }

        if(isOrgInDeYang(id)){
            return true;
        }

        if(isOrgInMeiShan(id)){
            return true;
        }

        // System.out.println("orgid not in hotcity:" + id);
        return false;
    }

    static public boolean isOrgInMianYang(int id){
        // select id from organization where id= 167681 and CityCode = (select
        // org.id from organization as org where org.NumCode = 3 );

        if(Db.find("select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where " + Organization.ID
                + "=" + id + " and " + Organization.CITYCODE + " = "
                + "(select org.id from organization as org where org.NumCode = ?)","3").size() <= 0){
            return false;
        } else{
            // System.out.println("orgid in mianyang:" + id);
            return true;
        }
    }

    static public boolean isOrgInDeYang(int id){
        // select id from organization where id= 167681 and CityCode = (select
        // org.id from organization as org where org.NumCode = 17 );

        if(Db.find("select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where " + Organization.ID
                + "=" + id + " and " + Organization.CITYCODE + " = "
                + "(select org.id from organization as org where org.NumCode = ?)","17").size() <= 0){
            return false;
        } else{
            // System.out.println("orgid in deyang:" + id);
            return true;
        }
    }

    static public boolean isOrgInMeiShan(int id){
        // select id from organization where id= 167681 and CityCode = (select
        // org.id from organization as org where org.NumCode = 19 );

        if(Db.find("select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where " + Organization.ID
                + "=" + id + " and " + Organization.CITYCODE + " = "
                + "(select org.id from organization as org where org.NumCode = ?)","19").size() <= 0){
            return false;
        } else{
            // System.out.println("orgid in meishan:" + id);
            return true;
        }
    }

    /*
     * 获取当前用户的Id
     */
    static public int getAdminId(){
        ShiroPrincipal principal = (ShiroPrincipal)SecurityUtils.getSubject().getPrincipal();
        int adminId = Integer.parseInt(principal.getId());
        return adminId;
    }

    /*
     * 获取当前用户的角色等级ID
     */
    static public Integer getRoleLevel(){
        ShiroPrincipal principal = (ShiroPrincipal)SecurityUtils.getSubject().getPrincipal();
        int adminId = Integer.parseInt(principal.getId());
        String sql = " select level from sys_user_role as ur, sys_role as r where ur.user_id = ? and ur.role_id = r.id ";
        Integer level = Db.queryInt(sql, adminId);

        return level;
    }

    public static int getRoleType(){
        ShiroPrincipal principal = (ShiroPrincipal)SecurityUtils.getSubject().getPrincipal();
        int adminId = Integer.parseInt(principal.getId());
        String sql = "SELECT type FROM sys_user_role ur, sys_role r WHERE ur.user_id=? AND ur.role_id=r.id";
        Integer type = Db.queryInt(sql, adminId);
        return type;
    }

    /**
     * @Title: getIsGovEnterprise
     * @Description:判断当前账号是否是政企
     * @return int 1：是，0：否
     */
    public static int getGovEnterpriseFlag(){
        int roleType = getRoleType();
        if(roleType == Constant.ROLE_TYPE_GOV_ENTERPRISE){
            return 1;
        }
        return 0;
    }

    /*
     * 获取当前登录用户的机构id
     */
    static public int getOrgId(){
        int adminId = getAdminId();
        int orgId = Db.queryInt("SELECT u.org_id FROM sys_user u WHERE u.id=?", adminId);
        return orgId;
    }

    public static boolean isCityLevelOrg(int orgId){
        final Record record = Db.findFirst("SELECT * FROM organization WHERE id=? AND id=CityCode", orgId);
        return record != null;
    }

    public static List<Entry<String, String>> getOrgList(){
        return OrganizationTree.getOrganizationList(getOrgId());
    }

    /*
     * 获取顶级机构id
     */
    static public int getIdTop(){

        if(ID_TOP <= 0){
            ID_TOP = Db.findFirst("select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where "
                    + Organization.ORGTYPE + "=1").getInt(Organization.ID);
        }

        return ID_TOP;

        // Response res= Ssdb.use().get(Constant.Cache.ID_TOP);
        // if(res==null||res.asInt()<=0){
        // int id =Db.findFirst("select " + Organization.ID + " from " +
        // Constant.Table.ORGANIZATION + " where "
        // + Organization.ORGTYPE + "=1").getInt(Organization.ID);
        // Ssdb.use().set(Constant.Cache.ID_TOP, id);
        // return id;
        // }else{
        // return res.asInt();
        // }
    }

    /*
     * 获取顶级机构id
     */
    static public int getIdChengdu(){

        if(ID_CHENGDU <= 0){
            ID_CHENGDU = Db.findFirst("select " + Organization.ID + " from " + Constant.Table.ORGANIZATION + " where "
                    + Organization.NUMCODE + "=2").getInt(Organization.ID);
        }

        return ID_CHENGDU;

    }

    static public String getNumCodeOfCityByOrgId(String org_id){
        String sql = "select NumCode from organization where is_deleted = 0 and id = (select CityCode from organization where is_deleted = 0 and id = ?) ";

        Record numCode = Db.findFirst(sql, org_id);
        return (null == numCode) ? "" : numCode.get("NumCode");
    }

    static public String getNumCodeOfAreaByOrgId(String org_id){
        String sql = "select NumCode from organization where is_deleted = 0 and id = (select AreaCode from organization where is_deleted = 0 and id = ?) ";

        Record numCode = Db.findFirst(sql, org_id);
        return (null == numCode) ? "" : numCode.get("NumCode");
    }

    static public String getNumCodeOfStoreByOrgId(String org_id){
        String sql = "select NumCode from organization where is_deleted = 0 and id = ? ";

        Record numCode = Db.findFirst(sql, org_id);
        return (null == numCode) ? "" : numCode.get("NumCode");
    }

    /**
     * @Title: getOrderSeq
     * @Description:获取订单序列号，递增，每天第一次获取时重置为1
     * @return Stirng：0000001
     */
    static public String getOrderSeq(OrderType orderType){
        int base = 10000000;
        String sql = "select getSeqValue(" + orderType.getType() + ",'"
                + DateKit.toStr(new Date(), "yyyy-MM-dd HH:mm:ss") + "') as seq";

        List<Record> seq = Db.find(sql);
        if(seq != null && seq.size() > 0){
            if(seq.get(0) != null){
                return String.valueOf(base + seq.get(0).getInt("seq")).substring(1);
            }
        }

        return String.valueOf(base).substring(1);
    }

    /**
     * @Title: getChengduOrgId
     * @Description:获取成都地区机构ID
     * @return int
     */
    public static int getCityOrgId(String cityName){
        Record record = Db.findFirst(String.format("select id from organization where DisplayName='%s'", cityName));
        if(record == null){
            return 0;
        } else{
            return record.getInt("id");
        }
    }

    /**
     * @Title: getCityNameByUserId
     * @Description:
     * @return int
     */
    public static String getCityNameByUserId(String userId){
        Record record = Db.findFirst(" select DisplayName from organization as g, "
                + " (select CityCode from member_org as mb, organization as org where mb.org_id = org.id and mb.id = ?) as cc "
                + " where g.id = cc.CityCode ", userId);
        if(record == null){
            return "";
        } else{
            return record.getStr("DisplayName");
        }
    }

    /**
     * @Title: getOrgIdByUserId
     * @Description:
     * @return int
     */
    public static String getOrgIdByUserId(String userId){
        Record record = Db.findFirst(" select org_id from member_org where id = ? ", userId);
        if(record == null){
            return "";
        } else{
            return record.getInt("org_id").toString();
        }
    }

    /**
     * @Title: getuserInfoByToken
     * @Description:
     * @return int
     */
    public static Record getuserInfoByToken(String token){
        Record record = Db.findFirst(
                                     " select m.* from user_token_map u join member_org m on u.username=m.user_name where u.token=? ",
                                     token);
        return record;
    }

    static public List<Record> getAllCity(){
        String sql = " select * from cmcc_city order by id ASC ";
        List<Record> seq = Db.find(sql);
        return seq;
    }

    public static Record getInfoByOrg(int org_id){
        /**
         * 获取organization信息
         */
        Record organization = Db.findById("organization", org_id);
        if(ObjectUtil.isEmpty(organization)){
            return null;
        }
        /**
         * 获取城市/地市Id,名称
         */
        StringBuffer sqlBuffer = new StringBuffer(" SELECT cc.id as city_id,cc.region_name as city_name");
        sqlBuffer.append(" FROM organization o  ");
        sqlBuffer.append(" JOIN  cmcc_city cc ON o.DisplayName = cc.region_name ");
        sqlBuffer.append(" WHERE o.id=? ");
        List<Record> list = Db.find(sqlBuffer.toString(), organization.getInt("CityCode"));
        /**
         * 获取区县信息
         */
        String areaSql = " SELECT DisplayName as displayName FROM  organization where id=?";
        List<Record> areaList = Db.find(areaSql, organization.getInt("AreaCode"));
        if(list == null || list.size() == 0){
            return null;
        } else{
            Record result = list.get(0);
            result.set("channel", organization.getStr("DisplayName"));// 获取渠道数据
            if(ObjectUtil.isNotEmpty(areaList) && areaList.size() > 0){
                Record areaRecord = areaList.get(0);
                result.set("displayName", areaRecord.getStr("displayName"));
            }
            return result;
        }
    }



    public static String getSubSqlByListPapameter(List<? extends Object> list){
        StringBuffer sb = new StringBuffer("(");
        list.forEach(obj ->{
            if(obj instanceof Integer || obj instanceof Float || obj instanceof Double || obj instanceof Long){
                sb.append(obj).append(",");
            }else if(obj instanceof  String){
                sb.append("'").append(obj).append("'").append(",");
            }
        });
        String sql = sb.toString();
        if(sql.endsWith(",")){
            sql = sql.substring(0,sql.length()-1);
        }

        sql+=")";
        return sql.length()>2?sql:"";
    }
}
